# -*- coding: utf-8 -*-

"""Create an application instance."""

from api.app import create_app

app = create_app()

class User01(object):

    def __init__(self, name, age, date):
        self.name = name
        self.age = age
        self.date = date

    def __repr__(self):
        return f"<User01 {self.name}>"

    def to_dict(self):
        return {"name": self.name, "age": self.age}
    

@app.route("/")
def hello_world():
    return "I am a chinese 000 中国人!"

import datetime
import decimal
from flask import jsonify
from werkzeug.exceptions import NotFound

@app.route("/json")
def _user01():
    user01 = User01('张三', 12, datetime.datetime.now())
    user02 = User01('张三2', 122, datetime.date.today())

    userList = [user01, user02]

    return jsonify([u.to_dict() for u in userList])
    # return jsonify(user01.to_dict())
    # return jsonify(user01.to_dict())
    # return {
    #     'name': user01.name,
    #     'age': user01.age,
    #     'date1': user01.date
    # }


class CustomExcept(Exception):
    pass


############################ 测试统一异常 ############################

# @app.route('/aaa')
# def raise_exception():
#     app.logger.error('数据格式错误')
#     raise  CustomExcept("抛出自定义异常错误")


# @app.errorhandler(404)
# def error_404(error):
#     print(f"发生了404错误，错误类型是：{type(error)}")
#     return 'This page does not exist', 404
    

# @app.errorhandler(500)
# def error_500(error):
#     print(f"发生了500错误，错误类型是：{type(error)}")
#     return 'server internal error', 500




